<template>
	<div class="invite-wrap">
    <div class="invite-top">
      <div class="invite-top-item">
        <div class="invite-top-item-left">
          <div class="img">
            <img :src="goodInfo.is_compress === 1 ? goodInfo.pic_url + '180x180' : goodInfo.pic_url" alt="" mode="scaleToFill">
          </div>
        </div>
        <div class="invite-top-item-right">
          <div class="title">{{goodInfo.title}}</div>
          <div class="info">
            <p class="icon"><i>活动价</i></p>
            <div class="new-price">
              <span class="price" v-html="scaleGoodsPriceFn(goodInfo.pay_price, 'nowprice')"></span>
            </div>
            <div class="origin-price">
              <del>&yen;{{goodInfo.original_price}}</del>
            </div>
          </div>
        </div>
      </div>
      <div class="invite-top-item-process" v-if="barginInfo.has_limit!=1">
        <p class="time">
          <span v-if="countDownList" class="time-wrap"><span class="time-count" v-html="countDownList"></span>后结束</span>
          <span v-else>砍价已结束</span>
        </p>
        <div class="money">
          <p>已砍<span class="k-num">{{barginInfo.bargin_amount}}</span>元，剩余</p>
          <p><span class="s-num">{{goodInfo.pay_price}}</span>元</p>
        </div>
      </div>
      <div class="invite-tip" v-if="barginInfo.has_limit == 1 || barginInfo.has_bargin === 1" :style="noPaddingBottom ? 'padding-bottom:0px' : ''">
        <template v-if="barginInfo.bargaining_activity_status == 0">
          <span>活动已结束</span>
        </template>
        <template v-else>
          <span v-if="barginInfo.has_limit==1">
            <span v-if="isMyself">恭喜，已砍至最低价</span>
            <span v-else>已帮朋友砍至最低价</span>
          </span>
          <span v-else-if="barginInfo.has_bargin === 1">您已参与过砍价，请下次再来</span>
        </template>
      </div>

      <div class="invite-btn" v-if="barginInfo.bargaining_activity_status != 0">
        <template v-if="barginInfo.has_limit == 1">
          <template v-if="isMyself">
            <div class="btn orange-bg-btn" @click="nowBuyItem(goodInfo.item_id)">立即购买</div>
            <div class="btn red-border-btn" @click="navToDetail(goodInfo.item_id)">再砍一单</div>
          </template>
          <template v-else>
            <div class="btn orange-bg-btn" @click="navToDetail(goodInfo.item_id)">我也要砍价</div>
            <div class="btn red-border-btn" @click="nowBuyItem(goodInfo.item_id)">原价购买</div>
          </template>
        </template>
        <template v-else>
          <template v-if="isMyself">
            <wx-button class="btn orange-bg-btn" open-type="share">邀请好友砍一刀</wx-button>
            <div class="btn red-border-btn" v-if="barginInfo.has_bargin === 0 && barginInfo.has_limit ==0" @click="nowKanItem">帮自己砍一刀</div>
            <div class="btn orange-border-btn" @click="navToDetail(goodInfo.item_id)">再砍一单</div>
          </template>
          <template v-else>
            <div class="btn orange-bg-btn" v-if="barginInfo.has_bargin === 0 && barginInfo.has_limit ==0" @click="nowKanItem">帮好友砍一刀</div>
            <div class="btn red-border-btn" @click="navToDetail(goodInfo.item_id)">我也要砍价</div>
            <wx-button class="btn orange-border-btn wx" open-type="share" >邀请好友帮TA砍价</wx-button>
          </template>
          <div class="btn orange-border-btn" @click="nowBuyItem(goodInfo.item_id)">{{isMyself ? '立即购买' : '原价购买'}}</div>
        </template>
      </div>
      <div class="invite-img" v-if="barginInfo.is_display && goodInfo.pic !=''">
        <a @click="openLink(goodInfo.url)">
          <img  width="100%" :src="goodInfo.pic" alt="">
        </a>
      </div>
    </div>
    <div class="invite-bottom">
      <div class="invite-bottom-title">
        <span class="img"><img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin_info.png" alt=""></span>
        <span class="tit">砍价榜</span>
        <span class="img"><img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin_info.png" alt=""></span>
      </div>
      <div class="invite-bottom-wrap" v-if="barginList.length > 0">
        <div class="invite-bottom-wrap-kjb-item" v-for="(item,index) in barginList" :key="item.user_id" >
          <div class="user">
            <span class="user-num" v-html="getNum(index+1)"></span>
            <span class="user-img"><img :src="item.avatar" alt=""></span>
            <span class="user-name">{{item.nickname}}</span>
          </div>
          <div class="money">
            <i class="icon"></i>
            <span>砍价</span>
            <span>{{item.amount}}元</span>
          </div>
        </div>
      </div>
      <div class="invite-bottom-wrap" v-else>
        <p>暂无砍价信息</p>
      </div>
    </div>
    <van-overlay :show="sharePopShow">
      <div class="wrapper" @click="sharePopShow = false">
        <img src="https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargin_share.png" alt="">
      </div>
    </van-overlay>
    <!--sku弹层-->
    <sku-dialog :visible.sync="skuDialogVisible" :itemId="itemId"></sku-dialog>
    <!-- 授权弹出框 -->
    <small-login ref="smalllogin"></small-login>

    <!--关注二维码弹窗-->
    <shop-code ref="shopCode"></shop-code>
  </div>
</template>

<script>
  /**
   * @Author: yqe@360shop.cn
   * @Date: 2019-11-29 09:38:45
 * @Last Modified by: zhangmeng
 * @Last Modified time: 2020-08-17 13:48:43
   */
  import Vue from 'vue'
  import { CountDown } from 'vant'
  import { Overlay } from 'vant'
  Vue.use(CountDown)
  Vue.use(Overlay)
  import { mpShare, openPage } from '@/utils/utils'
  import small from '@/smallapp/small'
  import skuDialog from '../../components/skuDialog'
  import ShopCode from '@/components/ShopCode'
  import { mapGetters } from 'vuex'
  import { scaleGoodsPrice } from '@/utils/index.js'
  import { getBarginDetail, getBarginOrder } from '@/api/item/activity.js'
  import SmallLogin from '@/components/SmallLogin/smallLogin'
  export default Vue.extend({
    name: 'index',
    components: {
      skuDialog,
      ShopCode,
      SmallLogin
    },
    data() {
      return {
        sendData:{},
        time: 0,
        isShowKan: false,
        skuDialogVisible: false,
        sharePopShow: false,
        barginInfo: {},
        goodInfo: {},
        itemId: 0,
        shareInfo:{},
        barginList: [],
        timeData: {
          days: '00',
          hours: '00',
          minutes: '00',
          seconds: '00'
        },
        countDownList: '',
        interval: null,
        uid: '',
        noPaddingBottom: false, // 砍价已关闭并且砍价成功后paddingBottom为0
        isMyself: false // 判断是否是自己 然后显示不同文案
      }
    },
    computed: {
      ...mapGetters(['wxConfig'])
    },
    watch: {
      wxConfig(val) {
        // console.log(val, 'wxconfig有变化')
      }
    },
    created() {
      this.uid = Number(this.$route.query.uid)
    },
    mounted() {
        window.addEventListener('wxload', query => {
            // console.log(query, '分享参数')
            this.sendData= {
              id:query.id || this.$route.query.id,
              uid:query.uid ||this.$route.query.uid
            }
            small.HandleShareParams()
        })

        window.addEventListener('wxshow', () => {
          this.getBarginDetailFun(this.sendData)
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
          }
        })

        window.$$subscribe('loginReload', reload => {
            if (reload) {
              this.getBarginDetailFun(this.sendData)
            }
        })
    },
    methods: {
      openLink(url){
        openPage(url)
      },
      init() {
        this.sendData= {
          id:this.$route.query.id,
          uid:this.$route.query.uid
        }
        this.getBarginDetailFun(this.sendData)
      },
      getNum(num) {
        if (num < 10) {
          num = '0' + num
        }
        return num
      },

      /**
       * 商品价格 整数放大
       */
      scaleGoodsPriceFn(price, className) {
        return scaleGoodsPrice(price, className)
      },

      timeChage(timeData) {
        this.timeData = {
          days: this.timeFormat(timeData.days),
          hours: this.timeFormat(timeData.hours),
          minutes: this.timeFormat(timeData.minutes),
          seconds: this.timeFormat(timeData.seconds)
        }
      },

      /**
       * 分享弹出层
       */
      shareFriends(show) {
        this.sharePopShow = show
      },

      /**
       * 立即购买
       */
      nowBuyItem(id) {
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
            return false
          }
        const buy_need_fans = this.$store.getters.wxConfig.buy_need_fans
        const that = this
        if (buy_need_fans == 1) {
          that.$isFans(() => {
            this.$Error('请先等待商家绑定公众号')
            return false
          }, () => {
            that.$refs.shopCode.dialogVisible = true
            return false
          }, () => {
            this.skuDialogVisible = true
            this.itemId = id
          })
        } else {
          this.skuDialogVisible = true
          this.itemId = id
        }
      },

      /**
       * 我也要砍价
       */
      navToDetail(id) {
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
            return false
          }
        const buy_need_fans = this.$store.getters.wxConfig.buy_need_fans
        const that = this
        if (buy_need_fans == 1) {
          that.$isFans(() => {
            this.$Error('请先等待商家绑定公众号')
            return false
          }, () => {
            that.$refs.shopCode.dialogVisible = true
            return false
          }, () => {
            this.$JumpName(this, 'item-detail', { 'detailId': id })
          })
        } else {
          this.$JumpName(this, 'item-detail', { 'detailId': id })
        }
      },
      /**
       * 立即砍价
       */
      nowKanItem() {
          if (!small.checkLogin()) {
            this.$refs.smalllogin.openSmallLoginPop()
            return false
          }
        const buy_need_fans = this.$store.getters.wxConfig.buy_need_fans
        const that = this
        if (buy_need_fans == 1) {
          that.$isFans(() => {
            this.$Error('请先等待商家绑定公众号')
            return false
          }, () => {
            that.$refs.shopCode.dialogVisible = true
            return false
          }, () => {
            this.getBarginOrderFn()
          })
        } else {
          this.getBarginOrderFn()
        }
      },
      getBarginOrderFn() {
        const sendData = {}
        sendData.id = this.itemId
        sendData.share_id = this.barginInfo.share_id
        sendData.bargin_id = this.barginInfo.bargin_id
        getBarginOrder(sendData).then(res => {
          if (res.status === 1) {
            this.$Error(res.msg)
            this.init()
          } else {
            this.$Error(res.msg)
          }
        })
      },
      /**
       * 获取砍价详情
       */
      getBarginDetailFun(sendData) {
        getBarginDetail(sendData).then(res => {
          if (res.status === 1) {
            const endTime = res.data.info.end_time * 1000
            this.setIntervalTime(endTime)
            this.barginInfo = res.data
            this.goodInfo = res.data.info
            this.barginList = res.data.list
            this.itemId = Number(res.data.info.item_id)
            this.isMyself = res.data.pid == res.data.share_id
            this.noPaddingBottom = res.data.has_limit == 1 && res.data.bargaining_activity_status == 0
            this.shareInfo=res.data

            // TODO 小程序分享
            this.shareInfo = {
              title: res.data.jsapi_title,
              imgUrl: res.data.jsapi_img,
              shareParam: res.data.jsapi_url.split('?')[1]              
            }
            mpShare (true, false, '/goods/pages/itemBargainDetail/index', this.shareInfo,'/item-bargain-detail')
          } else {
            this.$Error(res.msg)
          }
        })
      },

      // 分享成功的回调
      shareSuccess() {
        // 用户确认后执行的回调函数
        setTimeout(() => {
          window.location.reload()
        }, 1000)
      },

      /**
       * 补全小于10的
       */
      timeFormat(param) {
        return param < 10 ? '0' + param : param
      },

      // 设置定时器做倒计时
      setIntervalTime(end) {
        this.interval = setInterval(() => {
          this.countDownList = this.getResidueTime(end)
        }, 1000)
      },
      // 获取剩余时间
      getResidueTime(end) {
        const nowtime = new Date().getTime() // 当前时间 毫秒数
        // const endTime = Date.parse(new Date(end.replace(/-/g, '/'))) // 结束时间  毫秒数
        const endTime = end
        const totalSeconds = (endTime - nowtime) / 1000 // 结束时间-当前时间 = 剩余多少时间
        let day = 0
        let hour = 0
        let minute = 0
        let second = 0
        if (totalSeconds > 0) {
          day = parseInt(totalSeconds / 3600 / 24) // 天
          hour = parseInt((totalSeconds / 3600) % 24) // 时
          minute = parseInt((totalSeconds / 60) % 60) // 分
          second = parseInt(totalSeconds % 60) // 秒
        } else {
          day = 0 // 天
          hour = 0 // 时
          minute = 0 // 分
          second = 0 // 秒
          clearInterval(this.interval)
        }
        hour = this.addZero(hour)
        minute = this.addZero(minute)
        second = this.addZero(second)
        const residueTime = `<span class="num day time-bg">${day}</span><span class="pot day">天</span><span class="time-bg">${hour}</span><span class="pot">:</span><span class="time-bg">${minute}</span><span class="pot">:</span><span class="time-bg">${second}</span>`
        
        return residueTime
      },
      // 补齐格式不满10加0
      addZero(n) {
        return n < 10 ? '0' + n : n
      }
    },
    beforeDestroy() { // 组件的销毁
      clearInterval(this.interval) // 清除定时器
      this.interval = null
      window.$$unsubscribe('loginReload')
    }
  })
</script>
<style lang="scss">
  .time {
    .time-bg {
      background-color: #1A1A1A;
      color: #fff;
      width: 42px;
      height: 38px;
      line-height: 38px;
      text-align: center;
      display: inline-block;
      border-radius: 4px;
    }
    .pot {
      margin: 0 10px;
    }
  }

  .van-count-down {
    display: inline-block;
  }
</style>
<style lang="scss">
  @import "src/styles/mixin";
  @import "src/styles/variables";
  .invite-wrap{
    padding: 40px 36px;
    position: relative;
    background-image: 
      url("https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargain-bg1.png"), 
      url("https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargain-bg2.png"), 
      url("https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargain-bg3.png"), 
      url("https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargain-bg4.png"), 
      url("https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargain-bg4.png"), 
      url("https://img.wifenxiao.com/h5-wfx/images/item/bargain/bargain-bg5.png"),
      linear-gradient(180deg, #FC4154 0%, #FF5D56 100%);
    background-repeat: no-repeat, no-repeat, no-repeat, no-repeat, no-repeat, no-repeat; 
    background-position: left -60px top -120px, left 390px top, right -60px top -110px, right top 280px, left -30px top 860px, right -4px top 992px, center;
    background-size: 340px 204px, 59px 59px, 348px 190px, 66px 66px, 66px 66px, 113px 103px, 100% 100%;
    min-height: 100vh;
    .invite-top{
      position:relative;
      background: #fff;
      border-radius: 8px;
      overflow: hidden;
      padding: 25px;
      margin-bottom: 40px;
      &-item{
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-bottom: 20px;
        &-left{
          margin-right: 20px;
          .img{
            width: 180px;
            height: 180px;
            img{
              width: 100%;
            }
          }
        }
        &-right{
          flex: 1;
          height: 180px;
          position: relative;
          padding: 10px 0;
          box-sizing: border-box;
          .title{
            color: #333;
            font-weight: bold;
            line-height: 38px;
            vertical-align: top;
            @include lineClamp(28px, 40px, 1);
            margin-bottom: 10px;
          }
          .info{
            .icon {
              color: #fff !important;
              transform: skewX(-10deg);
              background-color: #FE1B49;
              width: 96px;
              height: 36px;
              line-height: 36px;
              text-align: center;
              border-radius: 4px;
              margin-right: 10px;
              margin-bottom: 10px;
              i{
                display:inline-block;
                transform: skewX(10deg);
              }
            }
            .new-price{
              font-size: 0;
              margin-bottom: 10px;
            }
            .origin-price{
              font-size: 24px;
              color: #999;
            }
          }
        }
        &-process {
          width: 100%;
          text-align: center;
          padding: 40px 0 70px;
          font-size: 28px;
          color: #1A1A1A;
          .money {
            p:nth-child(1) {
              margin-bottom: 20px;
            }
            .k-num {
              font-size: 28px;
              color: #F21228;
              font-weight: bold;
              margin: 0 5px;
            }
            .s-num {
              color: #F21228;
              font-weight: bold;
              font-size: 64px;
              margin: 0 5px;
            }
          }
          .time {
            .time-wrap {
              margin-bottom: 60px;
              display: block;
            }
            .time-count {
              margin-right: 20px;
            }
            .time-bg {
              background-color: #1A1A1A;
              color: #fff;
              width: 42px;
              height: 38px;
              line-height: 38px;
              text-align: center;
              display: inline-block;
              border-radius: 4px;
            }
          }
        }
      }
      &-step{
        margin-top: 15px;
        &-img{
          width: 625px;
          margin: 0 auto;
          padding: 40px 0;
          img{
            width: 100%;
          }
        }
      }
    }
    .invite-img{
      border-radius:20px;
      overflow: hidden;
      a{
        display: block;
        img{
          display: block;
          width: 100%;
        }
      }
    }
    .invite-bottom{
      background:rgba(255,242,243,1);
      border-radius:8px;
      padding: 20px 0;
      &-title{
        text-align: center;
        padding: 20px 0;
        .tit{
          font-size: 32px;
          color: #F21228;
          font-weight: bold;
        }
        .img{
          @include alignTop;
          width: 18px;
          height: 22px;
          margin: 0 5px;
          img{
            vertical-align: -6px;
            width: 100%;
          }
        }
      }
      &-wrap{
        p {
            line-height: 40px;
            padding-left: 20px;
            font-size: 28px;
        }
        &-item{
          display: flex;
          flex-direction: row;
          padding: 20px;
          .icon{
            width: 40px;
            height: 40px;
            line-height: 38px;
            font-size: 26px;
            text-align: center;
            color: #fff;
            background: #F21228;
            margin-right: 15px;
            border-top-left-radius: 50%;
            border-top-right-radius: 50%;
            border-bottom-right-radius: 50%;
            font-weight: bold;
          }
          .text{
            flex: 1;
            font-size: 28px;
            line-height: 36px;
            color: #666;
            margin-top: 6px;
          }
        }
        &-kjb-item {
          display: flex;
          flex-direction: row;
          flex-wrap: nowrap;
          justify-content: space-between;
          border-top: 1px dashed #ff7979;
          margin: 0 25px;
          padding: 30px 0px;
          font-size: 26px;
          font-weight: bold;
          &:first-child {
            margin-top: 20px;
          }
          .user {
            .user-num{
              margin-right:10px;
              font-size: 32px;
              font-weight: bold;
              color: #F21228;
            }
            .user-img {
              width: 66px;
              height: 66px;
              display: inline-block;
              border-radius: 50%;
              vertical-align: -20px;
              margin: 0 5px;
              &>img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
              }
            }
          }
          .money {
            line-height: 66px;
            .icon{
              display:inline-block;
              width:68px;
              height:39px;
              margin-right:10px;
              vertical-align: -10px;
              background:url("https://img.wifenxiao.com/h5-wfx/images/item/bargain/icon_bargin.png") no-repeat;
              background-size:100% 100%;
            }
            &>span {
              &:last-child {
                color: #F21228;
              }
            }
          }
        }
      }
    }
    .invite-tip {
      width: 100%;
      text-align: center;
      color: #F98933;
      font-size: 40px;
      padding-bottom: 80px;
    }
    .invite-btn{
        margin-bottom: 70px;
      .btn{
        font-size: 36px;
        line-height: 36px;
        padding: 24px 0;
        text-align: center;
        // color: #fff;
        border-radius: 60px;
        margin-bottom: 40px;
        &:last-child {
          margin-bottom: 0;
        }
        &.orange-bg-btn {
          background: linear-gradient(90deg, #F64A24 0%, #F99535 100%);
          color: #fff;
        }
        &.orange-border-btn.wx{
          color: #ff9102;
        }
      }
    }
    .wrapper {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
</style>
